package com.fujimana.rockincat.restree.web;

import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

import com.fujimana.rockincat.restree.init.Init;

public class RestreeListener implements ServletContextListener, ServletRequestListener {
	
	@SuppressWarnings("unchecked")
	@Override
	public void contextDestroyed(ServletContextEvent event) {
		
		ServletContext servletContext = event.getServletContext();
		
		Enumeration<String> names = servletContext.getAttributeNames();
		while(names.hasMoreElements()) {
			String name = names.nextElement();
			servletContext.removeAttribute(name);
		}
		
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		
		Init init = new Init();
		
		init.configure(event.getServletContext());
		init.loadMessages();
		init.scan(Thread.currentThread().getContextClassLoader());
		
		event.getServletContext().setAttribute(init.getClass().getName(), init);
		
	}

	@Override
	public void requestDestroyed(ServletRequestEvent event) {
	}

	@Override
	public void requestInitialized(ServletRequestEvent event) {
	}

	
}
